package com.future.bantang.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.bantang.R;
import com.future.bantang.constants.BanTang;
import com.future.bantang.entity.ShoppingListEntity.DataEntity.ProductEntity;
import com.future.bantang.entity.ShoppingListEntity.DataEntity.ProductEntity.LikesListEntity;
import com.future.bantang.entity.ShoppingListEntity.DataEntity.ProductEntity.PicEntity;
import com.future.bantang.view.CircleImageView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 16-1-16.
 */
public class ShoppingListAdapter extends BaseAdapter<ProductEntity> {
    //产品图片的链接主机名
    private String mProductPicHost;
    //喜欢列表中图片链接的主机名
    private String mUserAvatrHost;



    private ImageLoader mImageloader;


    public ShoppingListAdapter(Context context,ImageLoader imageloader) {
        super(context);
        this.mImageloader = imageloader;

    }


    public void setProductPicHost(String productPicHost) {
        this.mProductPicHost = productPicHost;
    }

    public void setUserAvatrHost(String userAvatrHost) {
        this.mUserAvatrHost = userAvatrHost;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_shopping_list_selection,null);
            holder=new ViewHolder();
            ViewUtils.inject(holder,convertView);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        holder.setDatas(position);
        return convertView;
    }

    private class ViewHolder {
        @ViewInject(R.id.iv_index_shopping_list_selection)
        private ImageView ivIndex;
        @ViewInject(R.id.tv_title_shopping_list_selection)
        private TextView tvTitle;
        @ViewInject(R.id.tv_desc_shopping_list_selection)
        private TextView tvDesc;
        @ViewInject(R.id.iv_pic_shopping_list_selection_01)
        private ImageView ivPic01;
        @ViewInject(R.id.iv_pic_shopping_list_selection_02)
        private ImageView ivPic02;
        @ViewInject(R.id.tv_price_shopping_list_selection)
        private TextView tvPrice;
        @ViewInject(R.id.tv_like_num_shopping_list_selection)
        private TextView tvLikeNum;
        @ViewInject(R.id.iv_like_shopping_list_selection_01)
        private CircleImageView ivLike01;
        @ViewInject(R.id.iv_like_shopping_list_selection_02)
        private CircleImageView ivLike02;
        @ViewInject(R.id.iv_like_shopping_list_selection_03)
        private CircleImageView ivLike03;
        @ViewInject(R.id.iv_like_shopping_list_selection_04)
        private CircleImageView ivLike04;
        @ViewInject(R.id.iv_like_shopping_list_selection_05)
        private CircleImageView ivLike05;
        @ViewInject(R.id.iv_like_shopping_list_selection_06)
        private CircleImageView ivLike06;
        @ViewInject(R.id.iv_like_shopping_list_selection_07)
        private CircleImageView ivLike07;
        @ViewInject(R.id.iv_like_shopping_list_selection_08)
        private CircleImageView ivLike08;
        @ViewInject(R.id.tv_comment_shopping_list_selection)
        private TextView tvComment;
        @ViewInject(R.id.tv_like_shopping_list_selection)
        private TextView tvLike;
        @ViewInject(R.id.iv_buy_shopping_list_selection)
        private ImageView ivBuy;

        private CircleImageView[] imageViews;


        public void setDatas(int position){
            if (imageViews==null){
                imageViews=new CircleImageView[]{ivLike01,ivLike02,ivLike03,ivLike04
                        ,ivLike05,ivLike06,ivLike07,ivLike08};
            }
            if (mList.size()<position+1){
                return;
            }
            ProductEntity entity=mList.get(position);
            ivIndex.setImageResource(BanTang.ivRes[position]);
            tvTitle.setText(entity.getTitle());
            tvDesc.setText(entity.getDesc());
            tvPrice.setText(entity.getPrice());
            tvLikeNum.setText(entity.getLikes());
            tvComment.setText(entity.getComments());
            tvLike.setText(entity.getLikes());
            if (!TextUtils.isEmpty(mProductPicHost)){
                List<PicEntity> picUrls = entity.getPic();
                mImageloader.displayImage(mProductPicHost+picUrls.get(0).getP(),ivPic01);
                if (picUrls.size()>1){
                    mImageloader.displayImage(mProductPicHost+picUrls.get(1).getP(),ivPic02);
                    ivPic02.setVisibility(View.VISIBLE);
                }else {
                    ivPic02.setVisibility(View.GONE);
                }

            }
            if (!TextUtils.isEmpty(mUserAvatrHost)){
                int count=0;
                List<LikesListEntity> likesList = entity.getLikes_list();
                int likeNum=Integer.parseInt(entity.getLikes().trim());
                if (likeNum>8){
                    count=8;
                }else {
                    count=likeNum;
                }
                for (int i = 0; i < count; i++) {
                    mImageloader.displayImage(mUserAvatrHost+likesList.get(i).getA(),imageViews[i]);
                }

            }


        }

    }

}
